// pages/order/ownermanagement/ownermanagement.js
const app = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		searchValue: '',
		list: [],
		userId: '',
		realName: '',
		isLastPage: false,
		current: 1,
		size: 10,
		goodsOwnerUserId:''
	},
	onChange(event) {
		this.setData({
			userId: event.detail,
		});
		this.data.list.forEach(item => {
			if (item.userId == this.data.userId) {
				this.setData({
					realName: item.realName
				})
			}
		});
	},
	toAddWorker() {
		wx.setStorage({
			key: "ownerInfo",
			data: {
				id: this.data.userId,
				name: this.data.realName,
			},
			success: function () {
				wx.navigateBack(); //返回上一个页面
			}
		})
	},
	onSearch(e) {
		this.setData({
			current: 1,
			list: [],
			isLastPage: false,
			searchValue: e.detail
		})
		console.log('搜索', this.data.searchValue)
		this.getData();
	},
	getData() {
		let {current,size} =this.data
		let parmas = {
			companyId: app.globalData.userInfo.companyData.companyId,
			marketRoleId: 4,//货主
			keyword:this.data.searchValue,
			current,
			size,
		}
		wx.showLoading({
			title: "正在请求数据中"
		})
		wx.$apis.getMarketUser(parmas).then(res => {
			wx.stopPullDownRefresh();
			let old = this.data.list;
			let allList = res.data && res.data.records.map(item => {
				return item
			})
			if (allList && allList.length) {
				let newList = old.concat(allList)
				this.setData({
					list: newList
				})
				if (allList.length < size) {
					this.setData({
						isLastPage: true
					})
				}
			} else {
				this.setData({
					isLastPage: true
				})
			}
		}).catch(err => {
			wx.hideLoading()
		})

	},


	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let goodsOwnerUserId=options.goodsOwnerUserId
		this.setData({
		userId:goodsOwnerUserId,
		})
		this.getData();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {
		this.setData({
			current: 1,
			list: [],
			isLastPage: false
		})
		this.getData()
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		let {
			current,
			isLastPage
		} = this.data
		if (isLastPage) {
			return
		}
		this.setData({
			current: (Number(current) + 1)
		})
		this.getData()
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})